<?php
session_start();

require_once('../configs/common.php'); 	 
require_once("../configs/db_connect.php");
require_once('../models/MyAccount.class.php');

if ($_POST['action']=="get_personal_data")
{
	 $userModel=new MyAccount();
	 header('Content-Type: application/json');
	 echo json_encode($userModel->getUserData());

}

if ($_POST['action']=="save_personal_data")
{
	 $userData=safe_var_Array(json_decode($_POST['data']), true);
	 $userData['federation']=(array)$userData['federation'];
	 
	 $response=array();
	 
	 if (MyAccount::saveUserData($userData))
	 {
		 $response['status']='ok';
	 }
	 else
	 {
		 $response['status']='Error during saving User\'s Data';
	 }
	 
	 header('Content-Type: application/json');
	 echo json_encode($response);

}

if ($_POST['action']=="get_all_albums")
{
	 $response=array();
     $request_data=safe_var_Array(json_decode($_POST['data']), true);
     if ($request_data['album_type']=='photo')
     {
         $response=MyAccount::getUserAlbums('photoalbums');
     }
     else
     {
         $response=MyAccount::getUserAlbums('videoalbums');
     }
     
     if (!$response)
     {
         $response['status']='no albums';
         $response['album_type']=$request_data['album_type'];
     }
     
	 header('Content-Type: application/json');
	 echo json_encode($response);
}

if ($_POST['action']=="get_albums_of_user")
{
	 $response=array();
     $request_data=safe_var_Array(json_decode($_POST['data']), true);
     if ($request_data['album_type']=='photo')
     {
         $response=MyAccount::getUserAlbums('photoalbums', $request_data['user_id']);
     }
     else
     {
         $response=MyAccount::getUserAlbums('videoalbums', $request_data['user_id']);
     }
     
     if (!$response)
     {
         $response['status']='no albums';
         $response['album_type']=$request_data['album_type'];
     }
     
	 header('Content-Type: application/json');
	 echo json_encode($response);
}

if ($_POST['action']=="create_new_photoalbum")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::createAlbum($request_data['photoalbum_name']);     
}

if ($_POST['action']=="delete_photo_album")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::deleteAlbum($request_data['id']);     
}

if ($_POST['action']=="edit_photoalbum_name")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::editAlbum($request_data['id'], $request_data['photoalbum_name']);     
}

if ($_POST['action']=="create_new_videoalbum")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::createAlbum($request_data['photoalbum_name'], 'videoalbums');     
}

if ($_POST['action']=="delete_video_album")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::deleteAlbum($request_data['id'], 'videoalbums');     
}

if ($_POST['action']=="edit_videoalbum_name")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::editAlbum($request_data['id'], $request_data['photoalbum_name'], 'videoalbums');     
}

if ($_POST['action']=="get_photos_from_album")
{
	 $response=array();
     $request_data=safe_var_Array(json_decode($_POST['data']), true);
     $_SESSION['photoalbum_id']=$request_data['photoalbum_id'];
	 
	 if ($request_data['user_id']=="my_id")
	 {
		 $response['photos']=MyAccount::getPhotosFromAlbum($request_data['photoalbum_id']);
		 $response['source']='my'; 
	 }
     else
	 {
		 $response['photos']=MyAccount::getPhotosFromAlbum($request_data['photoalbum_id'], $request_data['user_id']);
		 $response['source']='user_profile'; 
	 }
      if (!$response['photos'])
     {
         $response['status']='no photos';         
     }
     header('Content-Type: application/json');
	 echo json_encode($response);            
}

if ($_POST['action']=="get_videos_from_album")
{
	 $response=array();
     $request_data=safe_var_Array(json_decode($_POST['data']), true);
     $_SESSION['videoalbum_id']=$request_data['videoalbum_id'];
	 
	 if ($request_data['user_id']=="my_id")
	 {
		 $response['videos']=MyAccount::getVideosFromAlbum($request_data['videoalbum_id']);
		 $response['source']='my'; 
	 }
	 else
	 {
		 $response['videos']=MyAccount::getVideosFromAlbum($request_data['videoalbum_id'], $request_data['user_id']);
		 $response['source']='user_profile';
	 }
     
      if (!$response['videos'])
     {
         $response['status']='no videos';         
     }
     header('Content-Type: application/json');
	 echo json_encode($response);            
}

if ($_POST['action']=="new_photo_name")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::updatePhotoOrVideoName($request_data['id'], $_SESSION['user_id'], 'photos', $request_data['title']);     
}

if ($_POST['action']=="new_video_info")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::updatePhotoOrVideoName($request_data['key'], $_SESSION['user_id'], 'videos', $request_data['title']);     
}

if ($_POST['action']=="delete_current_photo")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::deletePhotoOrVideo($request_data['key'], $_SESSION['user_id'], 'photos');     
}

if ($_POST['action']=="delete_current_video")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     MyAccount::deletePhotoOrVideo($request_data['key'], $_SESSION['user_id'], 'videos');     
}

if ($_POST['action']=="upload_video")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
     $response=array();
          
     $resp = MyAccount::uploadVideo($request_data['url_yourtube'], $_SESSION['user_id'],  $_SESSION['videoalbum_id'], time());    
     if (!$resp)
     {
         $response['status']='not uploaded';             
     }
     else
     {
         $response['title']='Без названия';
         $response['link_key']=$resp;
         $response['status']='ok';   
     }
     header('Content-Type: application/json');
	 echo json_encode($response);  
}

if ($_POST['action']=="add_to_friends")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
	 if (!is_numeric($request_data['friend_id']))
	 {
		 $response['status']='no';
	 }
	 else
	 {
		 $request_data['friend_id']=(int) $request_data['friend_id'];
	 }
	 if (!is_int($request_data['friend_id']))
	 {
		 $response['status']='no';
	 }
	 else
	 {
		 MyAccount::addToFriends($request_data['friend_id']); 
		 $response['status']='added';
	 }
     	  
	 header('Content-Type: application/json');
	 echo json_encode($response);	 
}

if ($_POST['action']=="delete_from_friends")
{
	 $request_data=safe_var_Array(json_decode($_POST['data']), true);
	 if (!is_numeric($request_data['friend_id']))
	 {
		 $response['status']='no';
	 }
	 else
	 {
		 $request_data['friend_id']=(int) $request_data['friend_id'];
	 }
	 if (!is_int($request_data['friend_id']))
	 {
		 $response['status']='no';
	 }
	 else
	 {
		 MyAccount::deleteFromFriends($request_data['friend_id']); 
		 $response['status']='deleted';
	 }
     	  
	 header('Content-Type: application/json');
	 echo json_encode($response);	 
}

